import { defineStore } from 'pinia'
import { ref } from 'vue'
import {IAdmin} from '../services/administator/type'
import {checkLogin} from '../services/administator/admin'
import {convertToTree} from '../utils/treeUtils'
import { useRouter } from 'vue-router'
//有两个参数，第一个是唯一标识,
//第二个参数 对象，函数

export const useUserStore = defineStore("user", () => {

    const router=useRouter();
    const token = ref("");
    const menus:any = ref([]);
    const updateToken = (val: string) => {
        token.value = val;
    }
    const setMenus = (list: any) => {
        menus.value = list;
    }
    const login=async (data:IAdmin)=>{

        const res:any=await checkLogin(data);
        if(res.errCode===10000){

            let _menus=convertToTree(res.data.menu);

            console.log(_menus);
            
            token.value=res.data.token;
            menus.value=_menus;
            
            router.push("/");

        }
        else{
            //提示
        }



    }

    return {
        token, updateToken, menus, setMenus,login
    }

},{
    persist: {
        enabled: true,
        strategies: [
            {storage: localStorage}
        ]
    }
}
);

// export const useUserStore= defineStore("user",{
//     state:()=>{

//         return {
//             token:"aaaaaa",
//             menus:[]
//         }

//     },
//     getters:{

//         getToken:(state)=>{

//             return "token："+state.token.toUpperCase();
//         }

//     },
//     actions:{
//         updateToken(val:string){
//            this.token=val;
//         }
//     }


// })